package com.example.day0818_sever;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class ForBackgroud_ServiceAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    @Override
    public void onStart(Intent intent) {
        new ToastDialog(this).setText("onStart").show();
        HiLog.error(LABEL_LOG, "ForBackgroud_ServiceAbility::onStart");
        super.onStart(intent);
        startForground();
    }

    @Override
    public void onBackground() {
        new ToastDialog(this).setText("onBackground").show();
        HiLog.info(LABEL_LOG, "ForBackgroud_ServiceAbility::onBackground");
        super.onBackground();
    }

    @Override
    public void onStop() {
        new ToastDialog(this).setText("onStop").show();
        super.onStop();
        HiLog.info(LABEL_LOG, "ForBackgroud_ServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        new ToastDialog(this).setText("onCommand").show();

    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        new ToastDialog(this).setText("onConnect").show();

        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
        new ToastDialog(this).setText("onDisconnect").show();

    }
    private void startForground(){
        // 创建通知，其中1005为notificationId
        NotificationRequest request = new NotificationRequest(1005).setTapDismissed(true);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("这是一个前台服务").setText("略略略略略略略略略略略略.......");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);

        // 绑定通知，1005为创建通知时传入的notificationId
        keepBackgroundRunning(1005, request);
        new ToastDialog(this).setText("startForground").show();
    }

}